package com.wanmait.movies.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;

public class MovieLanguageDao {

	private MovieLanguageDao() {};
	
	private static MovieLanguageDao movieLanguageDao = new MovieLanguageDao();
	
	public static MovieLanguageDao getDao() {
		return movieLanguageDao;
	}
	//修改语言
		 public void updateLanguage(String name,int id){
				
				Connection conn = DBHelper.getDBHelper().getconn();
				PreparedStatement ps =null;
				
				String sql = "UPDATE movie_language set language_type=? where id=? ";
				try {
					ps =conn.prepareStatement(sql);
					ps.setString(1, name);
					ps.setInt(2, id);
				
					ps.executeUpdate();
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
					DBHelper.getDBHelper().close(null, ps, conn);
				}

		   }
	
	//通过id获得
	   public MovieLanguage findById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			ResultSet rs =null;
			
			String sql = "select * from movie_language where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
				
				rs=ps.executeQuery();
				if(rs.next()) {
				
				MovieLanguage a= new MovieLanguage();
				a.setLanguageType(rs.getString("language_type"));
				a.setId(id);
				
				return a;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(rs, ps, conn);
			}
			return null;
	   }
	//删除演员
	   public void deleteById(int id){
			
			Connection conn = DBHelper.getDBHelper().getconn();
			PreparedStatement ps =null;
			
			String sql = "delete from movie_language where id=?";
			try {
				ps =conn.prepareStatement(sql);
				ps.setInt(1, id);
			
				ps.executeUpdate();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally {
				DBHelper.getDBHelper().close(null, ps, conn);
			}

	   }
	
	//获得电影语言列表
			public ArrayList<MovieLanguage> getLanguageList(){
				ArrayList<MovieLanguage> list =new ArrayList<MovieLanguage>();
				
				Connection conn = DBHelper.getDBHelper().getconn();
				PreparedStatement ps =null;
				ResultSet rs =null;
				
				String sql ="select * from movie_language";
				
				try {
					ps = conn.prepareStatement(sql);
					
					rs = ps.executeQuery();
					
					while(rs.next()) {
						MovieLanguage movieLanguage =new MovieLanguage();
						
						movieLanguage.setId(rs.getInt("id"));
						movieLanguage.setLanguageType(rs.getString("language_type"));
						
						list.add(movieLanguage);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally {
					DBHelper.getDBHelper().close(rs, ps, conn);
				}
				return list;
			}
			
			//添加新语言
			   public void add(String language){
					
					Connection conn = DBHelper.getDBHelper().getconn();
					PreparedStatement ps =null;
					ResultSet rs =null;
					
					String sql = "insert into movie_language(language_type) values(?)";
					try {
						ps =conn.prepareStatement(sql);
						ps.setString(1, language);
						
						ps.executeUpdate();
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finally {
						DBHelper.getDBHelper().close(rs, ps, conn);
					}
					
			}
	//通过id获得语言
			   public MovieLanguage getLanguage(int id){
					
				   MovieLanguage language = new MovieLanguage();
					Connection conn = DBHelper.getDBHelper().getconn();
					PreparedStatement ps =null;
					ResultSet rs =null;
					
					String sql = "select * from movie_language where id=?";
					try {
						ps =conn.prepareStatement(sql);
						ps.setInt(1, id);
						
					while(rs.next()) {
						language.setLanguageType(rs.getString("language_type"));
						language.setId(id);
					}	
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finally {
						DBHelper.getDBHelper().close(rs, ps, conn);
					}
					return language;
			}
}
